package com.houtai.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.houtai.commonutils.Result;
import com.houtai.eduservice.client.VodClient;
import com.houtai.eduservice.config.OperationLogDesc;
import com.houtai.eduservice.entity.StuVideo;
import com.houtai.eduservice.entity.vo.VideoQuery;
import com.houtai.eduservice.service.StuVideoService;
import com.houtai.servicebase.exceptionhandler.DException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lin
 * @since 2022-12-09
 */
@RestController
@RequestMapping("/eduservice/stuvideo")
//@CrossOrigin
public class StuVideoController {
    @Autowired
    private StuVideoService stuVideoService;
    @Autowired
    private VodClient vodClient;
    //添加
    @PostMapping("addVideo")
    @ApiOperation(value = "添加绘本")
    @OperationLogDesc(module = "学习资源模块",description = "添加视频")
    public Result addVideo(@RequestBody @Valid StuVideo stuVideo){
        stuVideoService.save(stuVideo);
        return Result.ok();
    }

    @ApiOperation(value = "多条件组合查询带分页")
    @PostMapping("pageVideoCondition/{current}/{limit}")
    @OperationLogDesc(module = "学习资源模块",description = "查看视频列表")
    public Result pageVideoCondition(@PathVariable long current,
                                       @PathVariable long limit,
                                       @RequestBody(required = false) VideoQuery videoQuery){
        //创建page对象
        Page<StuVideo> pageT = new Page<>(current,limit);
        //调用方法实现分页
        //构建条件
        QueryWrapper<StuVideo> wrapper=new QueryWrapper<>();
        //wrapper
        //动态sql
        String title = videoQuery.getTitle();
        String begin = videoQuery.getBegin();
        String end = videoQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(title)){
            //构建条件
            wrapper.like("title",title);

        }
        if(!StringUtils.isEmpty(begin)){
            //构建条件
            wrapper.gt("gmt_create",begin);

        }
        if(!StringUtils.isEmpty(end)){
            //构建条件
            wrapper.le("gmt_create",end);

        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        stuVideoService.page(pageT,wrapper);

        long total = pageT.getTotal();//总记录数
        List<StuVideo> records = pageT.getRecords(); //数据list集合
        return Result.ok().data("total",total).data("rows",records);

    }
    //删除小节及其对于视频
    @DeleteMapping("{id}")
    @ApiOperation(value = "删除视频")
    @OperationLogDesc(module = "学习资源模块",description = "删除视频")
    public Result addVideo(@PathVariable Integer id){
        //根据小节ID得到视频ID
        StuVideo stuVideo= stuVideoService.getById(id);
        String videoSourceId=stuVideo.getVideoSourceId();
        //判断
        if(!com.alibaba.excel.util.StringUtils.isEmpty(videoSourceId)){
            Result result= vodClient.removeVideo(videoSourceId);
            if(result.getCode()==20001){
                throw new DException(20001,"删除失败，服务器挂掉啦！");
            }
        }
        stuVideoService.removeById(id);
        return Result.ok();
    }
    //修改小节
    //根据id查询
    @GetMapping("getVideoInfo/{videoId}")
    @OperationLogDesc(module = "学习资源模块",description = "获取视频信息")
    @ApiOperation(value = "获取信息")
    public Result getVideoInfo(@PathVariable Integer videoId){
        StuVideo stuVideo=stuVideoService.getById(videoId);
        return Result.ok().data("video",stuVideo);
    }
    @PostMapping("updateVideo")
    @OperationLogDesc(module = "学习资源模块",description = "修改视频信息")
    @ApiOperation(value = "修改")
    public Result updateVideo(@RequestBody @Valid StuVideo stuVideo){
        stuVideoService.updateById(stuVideo);
        return Result.ok();
    }

}

